<?php
//require_once(SERVER_ROOT.'app\\Idcard.php');
include_once SERVER_EXCEL;
class Studentsadd_ctl extends Ctl{
    function __construct(){
    }
    function main(array $getVars) {
        $type=!empty($_POST['type'])?$_POST['type']:false;
        if(!$type){
            $this::sessver();
            $this::show('studentsadd');
        }else{
            if($type=='upload'){
                    $targetPath = SERVER_ROOT.'html\\uploader\\xlsx'; // 指定的保存目录
                    if (!empty($_FILES)) {
                    	$tempFile = $_FILES['Filedata']['tmp_name'];
                    	// Validate the file type
                    	$fileTypes = array('xls','xlsx'); // File extensions
                    	$fileParts = pathinfo($_FILES['Filedata']['name']);
                    	$targetFile = rtrim($targetPath,'/') . '\\' . time().rand(10,99).".".$fileParts['extension'];
                     	if (in_array($fileParts['extension'],$fileTypes)) {
                    	    move_uploaded_file($tempFile,$targetFile);//保存文件到指定目录
                    		//move_uploaded_file($tempFile,iconv("UTF-8","gb2312", $targetFile));
                    		//下面是读取Excel文件并存入数据库
                    	    //$objPHPExcel = new PHPExcel();
                    	    //$PHPExcel = new PHPExcel();
                    	    $PHPReader = new PHPExcel_Reader_Excel2007();
                    	    $PHPExcel = $PHPReader->load($targetFile);
                    	    $currentSheet = $PHPExcel->getSheet(0);
                    	    $allColumn = $currentSheet->getHighestColumn();
                    	    $allRow = $currentSheet->getHighestRow();
                    	    if($currentSheet->getCell('A1')->getValue()!='x序号x'){
                    	        echo "模版有误，请选择正确的模版文件！";
                    	    }else{
                        	    $uid=$_POST['id'];
                        	    $array = array();
                        	    $array2 = array();
                        	    $i=0;$j=0;
                        	    for($currentRow = 2;$currentRow<=$allRow;$currentRow++){
                        	        $sname=trim($currentSheet->getCell('B'.$currentRow)->getValue());
                        	        if(strlen($sname)<2){echo 'B'.$currentRow."有误";exit();}
                        	        $idcard=trim($currentSheet->getCell('C'.$currentRow)->getValue());
                        	        if(!_checkIdCard($idcard)){echo 'C'.$currentRow."有误";exit();}
                        	        $id=xdb::getInstance()->getvar("select id from students where idcard = '$idcard'");
                        	        if($id){
                        	            $array['uid'] = $uid;
                        	            $array['sname'] = $sname;
                        	            $array['banji'] = trim($currentSheet->getCell('D'.$currentRow)->getValue());
                        	            $array['xuehao'] = trim($currentSheet->getCell('E'.$currentRow)->getValue());
                        	            $array['dizhi'] = trim($currentSheet->getCell('F'.$currentRow)->getValue());
                        	            $array['hukou'] = trim($currentSheet->getCell('G'.$currentRow)->getValue());
                        	            $array['ruxue'] = trim($currentSheet->getCell('H'.$currentRow)->getValue());
                        	            $array['zhuanye'] = trim($currentSheet->getCell('I'.$currentRow)->getValue());
                        	            $array['minzu'] = trim($currentSheet->getCell('J'.$currentRow)->getValue());
                        	            $array['zhengzhi'] = trim($currentSheet->getCell('K'.$currentRow)->getValue());
                        	            $array['jiankang'] = trim($currentSheet->getCell('L'.$currentRow)->getValue());
                        	            $array['shengyuan'] = trim($currentSheet->getCell('M'.$currentRow)->getValue());
                        	            $array['zhaosheng'] = trim($currentSheet->getCell('N'.$currentRow)->getValue());
                        	            $array['dianhua'] = trim($currentSheet->getCell('O'.$currentRow)->getValue());
                        	            $array['peiyang'] = trim($currentSheet->getCell('P'.$currentRow)->getValue());
                        	            $array['jiazhang'] = trim($currentSheet->getCell('Q'.$currentRow)->getValue());
                        	            $array['biyexuexiao'] = trim($currentSheet->getCell('R'.$currentRow)->getValue());
                        	            $array['xuejistatus'] = trim($currentSheet->getCell('S'.$currentRow)->getValue());
                        	            $array['xingshi'] = trim($currentSheet->getCell('T'.$currentRow)->getValue());
                        	            $array['xuezhi'] = trim($currentSheet->getCell('U'.$currentRow)->getValue());
                        	            $array['beizhu'] = trim($currentSheet->getCell('V'.$currentRow)->getValue());
                        	            $array2['id']=$id;
                        	            xdb::getInstance()->updb('students', $array, $array2);
                        	            $i++;
                        	        }else{
                        	            $array['uid'] = $uid;
                        	            $array['sname'] = $sname;
                        	            $array['xingbie'] = _getSexFromIdcard($idcard);
                        	            $array['shengri']=_getShengRiFromIdcard($idcard);
                        	            $array['idcard']=$idcard;
                        	            $array['banji'] = trim($currentSheet->getCell('D'.$currentRow)->getValue());
                        	            $array['xuehao'] = trim($currentSheet->getCell('E'.$currentRow)->getValue());
                        	            $array['dizhi'] = trim($currentSheet->getCell('F'.$currentRow)->getValue());
                        	            $array['hukou'] = trim($currentSheet->getCell('G'.$currentRow)->getValue());
                        	            $array['ruxue'] = trim($currentSheet->getCell('H'.$currentRow)->getValue());
                        	            $array['zhuanye'] = trim($currentSheet->getCell('I'.$currentRow)->getValue());
                        	            $array['minzu'] = trim($currentSheet->getCell('J'.$currentRow)->getValue());
                        	            $array['zhengzhi'] = trim($currentSheet->getCell('K'.$currentRow)->getValue());
                        	            $array['jiankang'] = trim($currentSheet->getCell('L'.$currentRow)->getValue());
                        	            $array['shengyuan'] = trim($currentSheet->getCell('M'.$currentRow)->getValue());
                        	            $array['zhaosheng'] = trim($currentSheet->getCell('N'.$currentRow)->getValue());
                        	            $array['dianhua'] = trim($currentSheet->getCell('O'.$currentRow)->getValue());
                        	            $array['peiyang'] = trim($currentSheet->getCell('P'.$currentRow)->getValue());
                        	            $array['jiazhang'] = trim($currentSheet->getCell('Q'.$currentRow)->getValue());
                        	            $array['biyexuexiao'] = trim($currentSheet->getCell('R'.$currentRow)->getValue());
                        	            $array['xuejistatus'] = trim($currentSheet->getCell('S'.$currentRow)->getValue());
                        	            $array['xingshi'] = trim($currentSheet->getCell('T'.$currentRow)->getValue());
                        	            $array['xuezhi'] = trim($currentSheet->getCell('U'.$currentRow)->getValue());
                        	            $array['beizhu'] = trim($currentSheet->getCell('V'.$currentRow)->getValue());
                        	            $array2['id'] = 0;
                        	            xdb::getInstance()->indb('students', $array, $array2);
                        	            $j++;
                        	        }
                        	    }
                        	    echo "处理结果：成功！<br>更新$i<br>新增$j";
                    	    }
                    	    //echo "未知错误！";
                    	} else {
                    		echo "模版有误，请选择正确的模版文件！";
                    	} 
                    }
            }else if($type=='pic'){
                $targetPath = SERVER_ROOT.'html\\uploader\\img'; // 指定的保存目录
                if (!empty($_FILES)) {
                	$tempFile = $_FILES['Filedata']['tmp_name'];
                	//$aid=$_POST['id'];
                	$fileTypes = array('jpg'); // File extensions
                	$fileParts = pathinfo($_FILES['Filedata']['name']);
                	$targetFile = rtrim($targetPath,'/') . '\\' .$fileParts['basename'];
                 	if (in_array($fileParts['extension'],$fileTypes)) {
                 	    $filename=rtrim($fileParts['basename'],'.'.$fileParts['extension']);
                 	    $id=xdb::getInstance()->getvar("select id from students where idcard='$filename'");
                 	    if($id){
                	       move_uploaded_file($tempFile,$targetFile);//保存文件到指定目录
                	       $array=array();$array2=array();
                	       $array['picstatus']='1';
                	       $array2['id']=$id;
                	       xdb::getInstance()->updb('students', $array, $array2);
                	       echo 1;
                 	    }
                 	}
                }
            }
        }
    }
}